package itbour.onetouchshow.fragment.challengezone;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.uilibrary.rv.RecyclerTabLayout;
import com.ys.uilibrary.vp.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.MVPBaseFragment;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.DesignTableLayout;


public class ChallengeZoneFragment extends MVPBaseFragment<ChallengeZoneContract.View, ChallengeZonePresenter> implements ChallengeZoneContract.View {

    @BindView(R.id.id_recyview)
    DesignTableLayout idRecyview;
    @BindView(R.id.id_vp_home)
    NoScrollViewPager mVpHome;
    private List<ChallengeZoneListFragment> challengeZoneListFragments = new ArrayList<>();
    private ChallengeZoneListFragment currentTypeFragment;

    private String[] mTitle = new String[]{"全部", "进行中", "评奖中", "已结束"};

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_challenge_zone;
    }

    @Override
    protected void afterCreate(Bundle bundle) {
        /**设计页面*/
        for (int i = 0; i < mTitle.length; i++) {
            ChallengeZoneListFragment designlistFragment = new ChallengeZoneListFragment();
            challengeZoneListFragments.add(designlistFragment);
        }
        PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager());
        mVpHome.setNoScroll(false);
        mVpHome.setAdapter(pagerAdapter);
        mVpHome.setCurrentItem(0);
        mVpHome.setOffscreenPageLimit(0);

        idRecyview.setUpWithAdapter(new IndicatorAdapter(mVpHome));
        idRecyview.setPositionThreshold(0.5f);
        idRecyview.setIndicatorColor(UIUtils.getColor(R.color.tab_selected));
        idRecyview.setIndicatorHeight(UIUtils.dip2px(2));
        idRecyview.setRecycleViewScollto(true);
        idRecyview.setAutoSelectionMode(true);

    }

    @Override
    protected void initTitle() {
        super.initTitle();
    }

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 三个Tab
     */
    public class IndicatorAdapter extends RecyclerTabLayout.Adapter<IndicatorAdapter.ViewHolder> {
        private PagerAdapter mAdapater;

        public IndicatorAdapter(ViewPager viewPager) {
            super(viewPager);
            mAdapater = (PagerAdapter) mViewPager.getAdapter();
        }

        @Override
        public void setCurrentIndicatorPosition(int indicatorPosition) {
            super.setCurrentIndicatorPosition(indicatorPosition);
            currentTypeFragment = challengeZoneListFragments.get(indicatorPosition);
        }

        @Override
        public IndicatorAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_design_view_tab, parent, false);
            return new IndicatorAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(IndicatorAdapter.ViewHolder holder, int position) {
            holder.text.setText(mTitle[position]);
            ViewGroup.LayoutParams layoutParams = holder.ry.getLayoutParams();
            // L_.e("WIDTH="+holder.text.getMeasuredWidth());
            if (mTitle.length > 0 && mTitle.length <= 5) {
                layoutParams.width = UIUtils.WH()[0] / mTitle.length;
            }
            if (position == getCurrentIndicatorPosition()) {
                holder.text.setTextColor(UIUtils.getColor(R.color.tab_selected));
            } else {
                holder.text.setTextColor(0xffffffff);
            }
        }


        @Override
        public int getItemCount() {
            return mAdapater.getCount();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView text;

            RelativeLayout ry;

            ViewHolder(View itemView) {
                super(itemView);
                text = itemView.findViewById(R.id.text);
                ry = itemView.findViewById(R.id.id_ry_root);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getViewPager().setCurrentItem(getAdapterPosition());
                    }
                });
            }
        }
    }

    public class PagerAdapter extends FragmentPagerAdapter {

        PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = challengeZoneListFragments.get(position);
            return fragment;
        }

        @Override
        public int getCount() {
            return challengeZoneListFragments.size();
        }

        @Override
        public String getPageTitle(int position) {
            return mTitle[position];
        }
    }
}
